package com.ydgk.dao;

import com.ydgk.bean.Pclass;
import com.ydgk.bean.Product;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

public class PclassDao extends BaseDao<Pclass> {
    @Override
    public int save(Pclass pclass) {
        return this.template.update("insert into y_pclass values(null,?)",pclass.getClassName());
    }

    @Override
    public int updateById(Pclass pclass) {
        return this.template.update("update y_pclass set className=? where id=?",pclass.getClassName(),pclass.getId());
    }

    @Override
    public int deleteById(int id) {
        return template.update("delete from y_pclass where id=?",id);
    }

    @Override
    public List<Pclass> findAll() {
        return this.template.query("select * from y_pclass",new BeanPropertyRowMapper<Pclass>(Pclass.class));
    }

    @Override
    public Pclass findById(int id) {
        return null;
    }

    @Override
    public List<Pclass> paging(int pageNumber) {
        return this.template.query("select * from y_pclass limit ?,?",new BeanPropertyRowMapper<Pclass>(Pclass.class),(pageNumber-1)*pageCount,pageCount);
    }
    public int maxPage(){
        String sql = "select count(*) from y_pclass";
        long t = template.queryForObject(sql, Long.class);
        int total = (int)t;//将long类型转换成int类型
        return total % pageCount == 0 ? total / pageCount : total / pageCount + 1 ;
    }
}
